#include "apue.h"

#include "detach.c"
#include "timeout.c"
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

void hello(void *in) {

  printf("延时执行完成\n");
  // return 0;
}

int main(void) {
  int err, condition, arg;
  struct timespec when;

  if ((err = pthread_mutexattr_init(&attr)) != 0) {
    //    err_exit(err, "pthread_mutexattr_init failed");
  }
  if ((err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)) != 0) {
    //    err_exit(err, "can't set recursive type");
  }
  if ((err = pthread_mutex_init(&mutex, &attr)) != 0) {
    //     err_exit(err, "can't create recursive mutex");
  }
  /* continue processing ... */

  pthread_mutex_lock(&mutex);

  condition = 1;
  /*
   * Check the condition under the protection of a lock to
   * make the check and the call to timeout atomic.
   */
  if (condition) {
    /*
     * Calculate the absolute time when we want to retry.
     */
    clock_gettime(CLOCK_REALTIME, &when);
    when.tv_sec += 2; /* 10 seconds from now */
    timeout(&when, retry, hello);
  }
  pthread_mutex_unlock(&mutex);

  /* continue processing ... */
  printf("执行完成");

  sleep(15);
  exit(0);
}